<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    kk_balance:'',
    title:'',
  })
  interface formQuert {
    custom_id:number
    game_name:string
    start_time:string
    end_time:string
    online_time:string
    if_rank:number
    if_lyb:number
    if_recharge:number
    if_build:number
    rank_gift_ids:string
    wealth_gift_id:string
    css:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    game_name: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
    start_time: [{ required: true, message: "请选择活动开始时间", trigger: "blur" }],
    end_time: [{ required: true, message: "请选择活动结束时间", trigger: "blur" }],
    online_time: [{ required: true, message: "请选择活动正式上线时间", trigger: "blur" }],
    rank_gift_ids: [{ required: true, message: "请输入榜单礼物ID", trigger: "blur" }],
    wealth_gift_id: [{ required: true, message: "请输入富豪值礼物ID", trigger: "blur" }],
    css: [{ required: true, message: "请输入样式文件名称", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { custom_id } = form
      if(custom_id) {
        await gamesApi.customEdit(form).then(()=>{
          ToastSuccess('编辑成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      else {
        await gamesApi.customAdd(form).then(()=>{
          ToastSuccess('新增成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any={})=> {
    const {
      id:custom_id=0, game_name='', start_time='', end_time='',
      online_time='', if_rank=0, if_lyb=0, if_recharge=0, if_build=0,
      rank_gift_ids='', wealth_gift_id='', css='',
    } = query
    const obj = {
      game_name, start_time, end_time,
      online_time, if_rank, if_lyb, if_recharge, if_build,
      rank_gift_ids, wealth_gift_id, css,
    } as any
    if(custom_id) obj['custom_id'] = custom_id
    Object.assign(form,obj)
    action.dialogFormVisible = true
    action.title = custom_id?`编辑活动：${game_name}`:'创建活动'
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    action.kk_balance = ''
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="52%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="活动名称" prop="game_name">
          <el-input v-model="form.game_name" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="开始时间" prop="start_time">
          <el-date-picker
            v-model="form.start_time"
            type="datetime"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择活动开始时间" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="结束时间" prop="end_time">
          <el-date-picker
            v-model="form.end_time"
            type="datetime"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择活动结束时间" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="正式上线时间" prop="online_time">
          <el-date-picker
            v-model="form.online_time"
            type="datetime"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择活动正式上线时间" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="活动模块">
          <el-checkbox v-model="form.if_rank" label="榜单" size="large" :true-value="1" :false-value="0"/>
          <el-checkbox v-model="form.if_build" label="盖楼" size="large" :true-value="1" :false-value="0" />
          <el-checkbox v-model="form.if_lyb" label="留言板" size="large" :true-value="1" :false-value="0" />
          <el-checkbox v-model="form.if_recharge" label="充值返现" size="large" :true-value="1" :false-value="0" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="样式文件名称" prop="css" >
          <el-input
            v-model="form.css"
            placeholder="请输入样式文件名称"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="富豪值礼物ID" prop="wealth_gift_id" >
          <el-input
            v-model="form.wealth_gift_id"
            placeholder="富豪值礼物ID"
            @input="allNumber(form,'wealth_gift_id')"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="榜单礼物ID" prop="rank_gift_ids" >
          <template  v-if="form.rank_gift_ids != 'all'">
            <el-input
              v-model="form.rank_gift_ids"
              :autosize="{ minRows: 6, maxRows: 6}"
              type="textarea"
              placeholder="榜单礼物ID英文逗号分隔"
            />
            <el-button class="xm-mt10" type="primary" @click="form.rank_gift_ids = 'all'">全部礼物</el-button>
          </template>
          <el-button v-else type="danger" @click="form.rank_gift_ids = ''">取消全部礼物</el-button>
        </el-form-item>
        
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>